package com.phf.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@ApiModel(
    value = "Result",
    description = "接口返回对象"
)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
//    public boolean isNeedI18n() {
//        return this.needI18n;
//    }
    //    @ApiModelProperty("是否在拦截器进行国际化转换")
//    private boolean needI18n = true;
    @Getter
    @ApiModelProperty("成功标志")
    private boolean success = true;
    @ApiModelProperty("返回处理消息")
    private String message = "success";
    @ApiModelProperty("返回代码")
    private Integer code = 0;
    @ApiModelProperty("返回数据对象")
    private T result;
    @ApiModelProperty("时间戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {
    }

    public static <T> Result<T> ok() {
        return wrapper(true, HttpStatus.OK.value(), "success", null);
    }

    public static <T> Result<T> ok(String msg) {
        return wrapper(true, HttpStatus.OK.value(), msg, null);
    }

    public static <T> Result<T> ok(T result) {
        return wrapper(true, HttpStatus.OK.value(), "operation.success", result);
    }

    public static <T> Result<T> ok(String msg, T result) {
        return wrapper(true, HttpStatus.OK.value(), msg, result);
    }

    public static <T> Result<T> error(String msg) {
        return wrapper(false, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, null);
    }

    public static <T> Result<T> error(String msg, T result) {
        return wrapper(false, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, result);
    }

    public static <T> Result<T> error(int code, String msg) {
        return wrapper(false, code, msg, null);
    }

    public static <T> Result<T> error(int code, String msg, T result) {
        return wrapper(false, code, msg, result);
    }

    public static <T> Result<T> error(HttpStatus status) {
        return wrapper(false, status.value(), status.getReasonPhrase(), null);
    }

    public static <T> Result<T> error(T result) {
        return wrapper(false, HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), result);
    }

    public static <T> Result<T> wrapper(boolean success, int code, String msg, T result) {
        Result<T> r = new Result<>();
        r.setSuccess(success);
        r.setCode(code);
        r.setMessage(msg);
        r.setResult(result);
        return r;
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = HttpStatus.OK.value();
        this.success = true;
        return this;
    }

    public Result<T> fail(String message) {
        this.message = message;
        this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        this.success = false;
        return this;
    }

//
//    public Result<T> setNeedI18n(boolean needI18n) {
//        this.needI18n = needI18n;
//        return this;
//    }

    public Result<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> setResult(T result) {
        this.result = result;
        return this;
    }

    public Result<T> setTimestamp(long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result)o;
            if (!other.canEqual(this)) {
                return false;
            }
//            else if (this.isNeedI18n() != other.isNeedI18n()) {
//                return false;
//            }
              else if (this.isSuccess() != other.isSuccess()) {
                return false;
            } else if (this.getTimestamp() != other.getTimestamp()) {
                return false;
            } else {
                label54: {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label54;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label54;
                    }

                    return false;
                }

                Object this$message = this.getMessage();
                Object other$message = other.getMessage();
                if (this$message == null) {
                    if (other$message != null) {
                        return false;
                    }
                } else if (!this$message.equals(other$message)) {
                    return false;
                }

                Object this$result = this.getResult();
                Object other$result = other.getResult();
                if (this$result == null) {
                    if (other$result != null) {
                        return false;
                    }
                } else if (!this$result.equals(other$result)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Result;
    }

    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
//        result = result * 59 + (this.isNeedI18n() ? 79 : 97);
        result = result * 59 + (this.isSuccess() ? 79 : 97);
        long $timestamp = this.getTimestamp();
        result = result * 59 + Long.hashCode($timestamp);
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $message = this.getMessage();
        result = result * 59 + ($message == null ? 43 : $message.hashCode());
        Object $result = this.getResult();
        result = result * 59 + ($result == null ? 43 : $result.hashCode());
        return result;
    }

    public String toString() {
//        return "Result(needI18n=" + this.isNeedI18n() + ", success=" + this.isSuccess() + ", message=" + this.getMessage() + ", code=" + this.getCode() + ", result=" + this.getResult() + ", timestamp=" + this.getTimestamp() + ")";
        return "Result(success=" + this.isSuccess() + ", message=" + this.getMessage() + ", code=" + this.getCode() + ", result=" + this.getResult() + ", timestamp=" + this.getTimestamp() + ")";
    }
}
